package cn.org.eshow.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * 红包代金券表
 */
@Entity
@Table(name = "coupon")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Coupon extends BaseObject implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;//红包代金券ID

    @Column(name = "add_time", nullable = false, length = 0)
    private Date addTime = new Date();//添加时间

    @Column(name = "start_time")
    private Date startTime;//开始时间

    @Column(name = "end_time")
    private Date endTime;//结束时间

    @Column
    private String title;//标题

    @Column
    private Double money ;//金额

    @Column(length = 50)
    private String scope = "不限";//使用范围（不限）

    @Column
    private Double requirement ;//使用条件（订单满多少分才能用这个代金券，单位元）

    @Column
    private Integer total ;//总库存数

    @Column
    private Integer stock ;//剩余可用库存数

    @Column
    private Integer used ;//已使用数

    @Column
    private Integer state;//状态（0未开始，-1已过期，2使用中）

    @Column
    private Boolean enabled = Boolean.TRUE;// 是否可用

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private User user;//用户ID

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Date getAddTime() {
        return addTime;
    }

    public void setAddTime(Date addTime) {
        this.addTime = addTime;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public Double getRequirement() {
        return requirement;
    }

    public void setRequirement(Double requirement) {
        this.requirement = requirement;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        this.stock = stock;
    }

    public Integer getUsed() {
        return used;
    }

    public void setUsed(Integer used) {
        this.used = used;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("id", this.id)
                .append("addTime", this.addTime)
                .append("startTime", this.startTime)
                .append("endTime", this.endTime)
                .append("money", this.money)
                .append("range", this.scope)
                .append("requirement", this.requirement)
                .append("state", this.state)
                .append("enabled", this.enabled)
                .append("user", this.user)
                .toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        final Coupon pojo = (Coupon) o;
        if (id != null ? !id.equals(pojo.id) : pojo.id != null)
            return false;
        if (addTime != null ? !addTime.equals(pojo.addTime) : pojo.addTime != null)
            return false;
        if (startTime != null ? !startTime.equals(pojo.startTime) : pojo.startTime != null)
            return false;
        if (endTime != null ? !endTime.equals(pojo.endTime) : pojo.endTime != null)
            return false;
        if (money != null ? !money.equals(pojo.money) : pojo.money != null)
            return false;
        if (scope != null ? !scope.equals(pojo.scope) : pojo.scope != null)
            return false;
        if (requirement != null ? !requirement.equals(pojo.requirement) : pojo.requirement != null)
            return false;
        if (state != null ? !state.equals(pojo.state) : pojo.state != null)
            return false;
        if (enabled != null ? !enabled.equals(pojo.enabled) : pojo.enabled != null)
            return false;
        if (user != null ? !user.equals(pojo.user) : pojo.user != null)
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        int result;
        result = (id != null ? id.hashCode() : 0);
        result = 31 * result + (addTime != null ? addTime.hashCode() : 0);
        result = 31 * result + (startTime != null ? startTime.hashCode() : 0);
        result = 31 * result + (endTime != null ? endTime.hashCode() : 0);
        result = 31 * result + (money != null ? money.hashCode() : 0);
        result = 31 * result + (scope != null ? scope.hashCode() : 0);
        result = 31 * result + (requirement != null ? requirement.hashCode() : 0);
        result = 31 * result + (state != null ? state.hashCode() : 0);
        result = 31 * result + (enabled != null ? enabled.hashCode() : 0);
        result = 31 * result + (user != null ? user.hashCode() : 0);
        return result;
    }
}
